package web.login;

import application.MySession;
import domain.User;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.jasypt.util.password.PasswordEncryptor;
import service.facade.user.UserService;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class ChangePasswordPanel extends Panel {


    @SpringBean
    private UserService userService;
    @SpringBean
    PasswordEncryptor passwordEncryptor;
    @SpringBean(name = "mySession")
    private MySession mySession;
    private PasswordWrapper pw = new PasswordWrapper();
    private static Logger logger = Logger.getLogger(ChangePasswordPanel.class.getName());

    public ChangePasswordPanel(String id) {
        super(id);
        final IModel<User> userModel = new LoadableDetachableModel<User>() {
            @Override
            protected User load() {
                return userService.findUserById(mySession.getId());
            }
        };
        Form mainForm = new Form("mainForm");
        add(mainForm);
        mainForm.add(new PasswordTextField("currentPassword", new PropertyModel(pw, "currentPassword")));
        mainForm.add(new PasswordTextField("newPassword1", new PropertyModel(pw, "newPassword1")));
        mainForm.add(new PasswordTextField("newPassword2", new PropertyModel(pw, "newPassword2")));
        mainForm.add(new Button("saveButton") {

            @Override
            public void onSubmit() {
                if (!passwordEncryptor.checkPassword(pw.getCurrentPassword(), userModel.getObject().getPassword())) {
                    error(getLocalizer().getString("password.fail.origPassword", this));
                } else if (!pw.getNewPassword1().equals(pw.getNewPassword2())) {
                    error(getLocalizer().getString("password.fail.password", this));
                } else {
                    try {
                        if (userService.changePassword(userModel.getObject(), pw.getCurrentPassword(), pw.getNewPassword1())) {
                            info(getLocalizer().getString("password.success.change", this));
                        } else {
                            error(getLocalizer().getString("password.change.error", this));

                        }
                        ;

                    } catch (Exception e) {
                        logger.log(Level.SEVERE, null, e);
                        error(getLocalizer().getString("system.error", this));
                    }
                }
                pw.clear();
            }
        });
    }


    private class PasswordWrapper implements Serializable {

        private static final long serialVersionUID = 1L;
        private String currentPassword;
        private String newPassword1;
        private String newPassword2;

        public String getNewPassword1() {
            return newPassword1;
        }

        public String getCurrentPassword() {
            return currentPassword;
        }

        public void setCurrentPassword(String oldPassword) {
            this.currentPassword = oldPassword;
        }

        public void setNewPassword1(String newPassword1) {
            this.newPassword1 = newPassword1;
        }

        public String getNewPassword2() {
            return newPassword2;
        }

        public void setNewPassword2(String newPassword2) {
            this.newPassword2 = newPassword2;
        }

        public void clear() {
            currentPassword = null;
            newPassword2 = null;
            newPassword1 = null;
        }
    }
}
